﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace ShenLan.Common
{
    public class WriteFile
    {
        /// <summary>
        /// 将异常信息写到文本
        /// </summary>
        /// <param name="ex">Exception ex</param>
        public static void WriteErrorToTxt(Exception ex)
        {
            try
            {
                string filePath = "ErrorLog.txt";
                System.IO.FileStream fs = new System.IO.FileStream(filePath, System.IO.FileMode.Append);
                System.IO.StreamWriter sw = new System.IO.StreamWriter(fs);
                sw.WriteLine("=================================");
                sw.WriteLine(DateTime.Now.ToString());
                sw.WriteLine("=================================");
                sw.WriteLine("获取一个提供用户定义的其他异常信息的键/值对的集合(Data):" + ex.Data);
                sw.WriteLine("获取或设置指向此异常所关联帮助文件的链接(HelpLink):" + ex.HelpLink);
                sw.WriteLine("获取导致当前异常的 Exception 实例(InnerException):" + ex.InnerException);
                sw.WriteLine("获取或设置导致错误的应用程序或对象的名称(Source):" + ex.Source);
                sw.WriteLine("获取描述当前异常的消息(Message):" + ex.Message.ToString());
                sw.WriteLine("获取调用堆栈上直接帧的字符串表示形式(StackTrace):" + ex.StackTrace);
                sw.WriteLine("获取引发当前异常的方法(TargetSite):" + ex.TargetSite);
                sw.WriteLine(" ");
                sw.Close();
                fs.Close();
            }
            catch
            {
            }
        }

        /// <summary>
        /// 根据传入路径和内容写到txt文件
        /// </summary>
        /// <param name="path">自定义文件路径</param>
        /// <param name="content">要写入文件的内容</param>
        public static void WriteToTxt(string path, string content)
        {
            try
            {                
                using (var fs = new System.IO.FileStream(path, System.IO.FileMode.Append))
                {
                    using (var sw = new System.IO.StreamWriter(fs, System.Text.Encoding.UTF8))
                    {
                        sw.WriteLine(content);
                    }
                }
            }
            catch { }
        }

        public void Test()
        {
            var start = DateTime.Now;
            ShenLan.Common.WriteFile.WriteToTxt("d:\\test.txt", start.ToString());
            for (var a = 0; a < 100000; a++)
            {
                var temp = "00000000" + a;
                temp = temp.Substring(temp.Length - 6, 6);
                ShenLan.Common.WriteFile.WriteToTxt("d:\\test.txt", temp);
            }
            var end = DateTime.Now;
            ShenLan.Common.WriteFile.WriteToTxt("d:\\test.txt", end.ToString());
            var second = (end - start).TotalSeconds;
            var minutes = (end - start).TotalMinutes;
            ShenLan.Common.WriteFile.WriteToTxt("d:\\test.txt", "写10,000次所用时间(秒):" + second.ToString());
            ShenLan.Common.WriteFile.WriteToTxt("d:\\test.txt", "写10,000次所用时间(分钟):" + minutes.ToString());           
        }
    }
}